<?php

class common_api {

    protected $db = null;

    public function __construct($db) {
	$this->db = $db;
    }

    private $msg_api='0';			//0是新的短信接口 1是深圳旧接口 

   public function check_ios_version($data){
	   
   }
   
   //ios ,add check version
	public function set_ios_log($data){
		$phone_imei = trim($data['imei']);		
		$model = trim($data['model']);
		$sdk = trim($data['sdk']);
		$channel = '0000001';				//ios固定
		$type = trim($data['type']);
		$version = trim($data['version']);
		 //记录IMEI码统计信息
        load_file('imei_class',LIB_PATH);
        $Imei = new Imei($this->db);
        switch($type){
            case 'open':    //打开软件               
			   $output = $Imei->open_ios_soft($phone_imei,$model,$version,$sdk,$channel);//添加版本检测			 
        		return $output;			   						
                break;
            case 'close':   //关闭软件
                $res = $Imei->close_soft($phone_imei);				
                break;          
            default:
                $output = array('status'=>'0','tips'=>'参数错误!');
				return $output;
                break;
        }
		
		
        
	}
   
   
   
    //检测版本
    public function check_version($data) {
	$output=array();
        //$sn = $data['sn'];
        $version = $data['version'];
        $sql = "select * from data_channel where channel_sn ='0000000' and  channel_version > " . $version;
        $row = $this->db->getRow($sql);

        if ($row) {
            $output['status'] = '1';
            $output['url'] = SITE_ADDR . trim($row['channel_apk_url'], "/");
            $output['version'] = $row['channel_version'];
            $output['title'] = '版本更新提示';
            $output['tips'] = $row['channel_update_info'];
            /*$output['tips'] = "发现新版本,请更新!\n如果安装失败,请卸载后再安装.\n\n"
            ."1.更新激情团购界面,展示效果更简洁 \n"
            ."2.简化团购使用流程，操作更简单\n";*/
        } else {
            $output['status'] = '0';
			$output['title'] = '版本提示';
            $output['tips'] = '当前版本已是最新版本!';
        }
        return $output;
    }

    public function get_page($data) {
	$page = $data['page'] ? $data['page'] : 1;
	$page_rows = $data['pagerows'] ? $data['pagerows'] : 5;
	$limit = "limit " . ($page - 1) * $page_rows . "," . $page_rows;
	return $limit;
    }

    public function clean_null($data){
        if(is_array($data)){
            foreach($data as $k=>$v){
                $data[$k] = $this->clean_null($v);
            }
        }
        if( $data === null  ){
            $res = '';
        }else{
            $res = $data;
        }
        return $res;
    }
    
    

    //设置发短信提供商
    public function set_msg_api($type='0'){
        if($type){
            $this->msg_api= $type;
        }else{
            $this->msg_api='0';
        }
    }
    
    //发送短信
    protected function send_sms($phone,$type,$uid='',$user='',$from_phone=''){
        if(empty($phone)){
            return false;
        }else{
            $code=random_code (4,'NUMBER'); //生成验证码
            require(CONFIG_PATH.'message_var.php');
            switch ($type){                
                case 0://0为忘记密码
                    $content = $cf_message_var['reset_password'];
                    break;
				case 1://1为手机验证
                    $content = $cf_message_var['checkphone'];
                    break;              
                default:
                    return false;
                    break;
            }
			
            if($this->msg_api){
                load_file('sms2_class', LIB_PATH);
            }else{                
				load_file('sms_class', LIB_PATH);
            }

            $sms=new Sms();
            $rs = $sms->sendSMS($phone,$content);
            if($rs){              
                //发送短信记录
                $sms_log = array(
                    'sms_number'=>$phone,
                    'sms_type'=>$type,
                    'sms_check_code'=>$code,
                    'sms_send_time'=>date('Y-m-d H:i:s'),
                );
                $this->db->insert('data_sms_log',$sms_log);
                return true;
            }else{
                return false;
            }
        }
    }

    //检查用户是否通过验证
    public function check_user_mobile($uid) {
        $row = $this->db->getRow("SELECT `user_check`,`user_phone` FROM data_user WHERE user_id='" . intval($uid) . "'");
        if ($row['user_check'] == '1') {
            //$row=array("status"=>'1');
        } else {
            $to_data = array('status' => '0', 'title' => '温馨提示', 'tips' => '您的帐户还没完善信息,请点击头像进行完善');
            return $to_data;
        }
        return $row;
    }

    //搜索关键字
    public function get_keyword($data){
        $mod = trim($data['mod']);
        $num = intval($data['num']);

        $list = $this->db->getAll("SELECT keyword_id,keyword_content FROM data_keyword WHERE keyword_cate='".$mod."' and keyword_show='1' order by keyword_sort limit ".$num);
        if($list){
            $output['status'] = 1;
            $output['list'] = $list;
        }else{
            $output['status'] = 0;
            $output['tips'] = "无关键字";
        }

        return $output;
    }
   
	//计算会员的级别
	public function get_level($count){
		$num = 50;//参加多少个调查可以升级
		$level = intval($count/$num);
		if($level > 4){
			$level = 4;
		}
		return $level;		
	} 
}

?>
